<?php
/**
 * @author songlin
 * @since 2017/10/24
 * 有关数组的一些操作
 */

define('EOL', '<br />');

//$data = [5, 1, 2, 3, 4, 1, 8, 10, 5, 8, 7, 6, 5];
//
//$result = dichotomy($data);
//var_dump($result);

$data = array(
    array('catid' => '1546', 'catname' => '续梦PHP 一级', 'count' => '588'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 二级', 'count' => '388'),
    array('catid' => '1546', 'catname' => '续梦PHP 三级', 'count' => '488'),
    array('catid' => '1546', 'catname' => '续梦PHP 三级', 'count' => '488'),
    array('catid' => '1546', 'catname' => '续梦PHP 三级', 'count' => '488'),
);

$i = 0;
$startTime = microtime(true);
do {
    $i++;
    array_sort2($data, 'count');
} while ($i < 100000);
$endTime = microtime(true);
echo $endTime - $startTime, EOL;

$i = 0;
$startTime = microtime(true);
do {
    $i++;
    array_sort($data, 'count');
} while ($i < 100000);
$endTime = microtime(true);
echo $endTime - $startTime, EOL;



/**
 * 合并两个可能为空的数组
 * @param $array1
 * @param $array2
 * @return array
 *
 * @author songlin
 */
function merge($array1, $array2)
{
    $array1 = (array)$array1;
    $array2 = (array)$array2;
    if (empty($array1)) {
        $result = $array2;
    } elseif (empty($array2)) {
        $result = $array1;
    } else {
        $result = array_merge($array1, $array2);
    }
    return $result;
}

/**
 * 将二维数组按指定字段排序
 * @param $array
 * @param string $keyword 排序字段
 * @param string $direction 升降排序 默认asc 升序排序
 * @return array
 *
 * @author songlin
 */
function array_sort($array, $keyword, $direction = 'asc')
{
    $temp = $result = array();
    foreach ($array as $k => $v) {
        $temp[$k] = $v[$keyword];
    }
    if ($direction == 'asc') {
        asort($temp);
    } else {
        arsort($temp);
    }
//    reset($temp); //将内部指针指向数组中的第一个元素
    foreach ($temp as $k => $v) {
        $result[$k] = $array[$k];
    }
    return $result;
}

/**
 * 将二维数组按指定字段排序
 * @param $array
 * @param string $keyword 排序字段
 * @param string $direction 排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
 * @return mixed
 *
 * @author songlin
 */
function array_sort2($array, $keyword, $direction = 'SORT_ASC')
{
    $temp = array();
    foreach ($array as $key => $value) {
        $temp[$key] = $value[$keyword];
    }

    array_multisort($temp, constant($direction), $array);
    return $array;
}

/**
 * 二分法排序数组
 * @param $array
 * @return array
 *
 * @author songlin
 */
function dichotomy($array)
{
    $left = [];
    $right = [];
    $length = count($array);

    if ($length > 1) {
        $index = $array[0];
        for ($i = 1; $i < $length; $i++) {
            $value = $array[$i];
            if ($index >= $value) {
                $left[] = $value;
            } else {
                $right[] = $value;
            }
        }

        $left = dichotomy($left);
        $right = dichotomy($right);

        array_push($left, $array[0]);
        $result = array_merge($left, $right);
        
        return $result;
    } else {
        return $array;
    }
}